package com.epam.web_project.bank.command.show_commands;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.epam.web_project.bank.command.Command;
import com.epam.web_project.bank.dao.factory.MySQLDAOFactory;
import com.epam.web_project.bank.dao.transaction_dao.TransactionDAO;
import com.epam.web_project.bank.db.ConnectionPool;
import com.epam.web_project.bank.entity.Account;
import com.epam.web_project.bank.entity.Transaction;
import com.epam.web_project.bank.util.AppPropertiesLoader;

public class ShowHistoryCommand implements Command {
	
	//show history on an account,
	//used by a regular customer
	@Override
	public String execute(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String resPage;
		AppPropertiesLoader props=AppPropertiesLoader.getInstance();
		if(!request.getParameter("accId").equals("")) {
			Account account=new Account();
			account.setId(Integer.valueOf(request.getParameter("accId")));
			MySQLDAOFactory daoFactory = new MySQLDAOFactory();
			daoFactory.setConnectionPool(ConnectionPool.getInstance());
			TransactionDAO transactionDAO=daoFactory.getTransactionDAO();
			List<Transaction> incomings=transactionDAO.findByAccountTo(account);
			List<Transaction> outgoings=transactionDAO.findByAccountFrom(account);
			request.setAttribute("incomings", incomings);
			request.setAttribute("outgoings", outgoings);
			resPage=props.getProperty("history_page");
		} else {
			resPage=props.getProperty("show_main_command");
		}
		return resPage;
		
	}
}
